export default {
	namespaced: true,
	state() {
		return {
			address: uni.getStorageSync('address') || null,
			token: uni.getStorageSync('token') || '',
			userProfile: uni.getStorageSync('userProfile') || {
				avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
				nickName: '请先登录'
			}
		};

	},
	getters: {
		fullAddress(state) {
			const { address } = state;
			//如果地址为空 将完整地址设置为空字符串
			if (!address) return '';
			//拼凑完整的地址
			return address.provinceName + address.cityName + address.countyName + address.detailInfo;
		}
	},
	actions: {
		async getToken() {
			// 获取登录凭证
			const [err1, { code }] = await uni.login();
			// 获取用户信息

			const [err2, { rawData, signature, iv, encryptedData }] = await uni.getUserInfo();
			// 调用接口获取 Token
			const { data: res } = await uni.$http.post('/api/public/v1/users/wxlogin', {
				code,
				rawData,
				signature,
				iv,
				encryptedData
			});
			console.log('接口token传入返回的', res);
			// 记录保存token
			if (res.meta.status === 200) this.commit('m_user/saveToken', res.message.token);

		}
	},
	mutations: {
		saveToken(state, token) {
			state.token = token;
			uni.setStorageSync('token', token);
		},
		saveAddress(state, address) {
			//将获取的收货地址记录下来
			state.address = address;
			// console.log(state.address);
			uni.setStorageSync('address', state.address);
		},
		saveProfile(state, userProfile) {
			state.userProfile = userProfile,
				uni.setStorageSync('userProfile', state.userProfile);
		}
	}



};
